package com.chx.service.Impl;

import com.chx.constant.StatusConstant;
import com.chx.context.BaseContext;
import com.chx.dto.*;
import com.chx.entity.PurchaseOrderDetails;
import com.chx.entity.PurchaseOrders;
import com.chx.entity.StockInOrderDetails;
import com.chx.entity.StockInOrders;
import com.chx.mapper.*;
import com.chx.result.PageResult;
import com.chx.service.PurchaseOrderService;
import com.chx.vo.PurchaseOrderDetailVo;
import com.chx.vo.PurchaseOrderVo;
import com.chx.wrapper.PurchaseOrderWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 */
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PurchaseOrderDetailMapper purchaseOrderDetailMapper;

    @Autowired
    private StockInOrderMapper stockInOrderMapper;
    @Autowired
    private StockInOrderDetailMapper stockInOrderDetailMapper;
    @Transactional
    @Override
    public void save(PurchaseOrderDto purchaseOrderDto) {
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        BeanUtils.copyProperties(purchaseOrderDto, purchaseOrders);
        Double  totalAmount = 0.0;
        //purchaseOrderDto.getPurchaseOrderDetailDtoList().forEach((purchaseOrderDetailDto)-> totalAmount += purchaseOrderDetailDto.getSubtotal());
        for(PurchaseOrderDetailDto purchaseOrderDetailDto:purchaseOrderDto.getPurchaseOrderDetailDtoList()){
            totalAmount += purchaseOrderDetailDto.getSubtotal();
        }
        purchaseOrders.setTotalAmount(totalAmount);
        purchaseOrderMapper.insert(purchaseOrders);


        PurchaseOrderDetails purchaseOrderDetails = new PurchaseOrderDetails();
        //获取订单id
        purchaseOrderDetails.setOrderId(purchaseOrders.getOrderId());

        for(PurchaseOrderDetailDto purchaseOrderDetailDto:purchaseOrderDto.getPurchaseOrderDetailDtoList()){
            BeanUtils.copyProperties(purchaseOrderDetailDto, purchaseOrderDetails);
            purchaseOrderDetailMapper.insert(purchaseOrderDetails);
        }
    }

    @Transactional
    @Override
    public void update(PurchaseOrderDto purchaseOrderDto) {
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        BeanUtils.copyProperties(purchaseOrderDto, purchaseOrders);
        Double  totalAmount = 0.0;
        for(PurchaseOrderDetailDto purchaseOrderDetailDto:purchaseOrderDto.getPurchaseOrderDetailDtoList()){
            totalAmount += purchaseOrderDetailDto.getSubtotal();
        }
        purchaseOrders.setTotalAmount(totalAmount);
        purchaseOrderMapper.update(purchaseOrders);

        purchaseOrderDetailMapper.deleteByOrderId(purchaseOrderDto.getOrderId());

        PurchaseOrderDetails purchaseOrderDetails = new PurchaseOrderDetails();
        //获取订单id
        purchaseOrderDetails.setOrderId(purchaseOrderDto.getOrderId());

        for(PurchaseOrderDetailDto purchaseOrderDetailDto:purchaseOrderDto.getPurchaseOrderDetailDtoList()){
            BeanUtils.copyProperties(purchaseOrderDetailDto, purchaseOrderDetails);
            purchaseOrderDetailMapper.insert(purchaseOrderDetails);

        }
    }

    @Override
    public void cancel(Long id) {
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        purchaseOrders.setStatus(StatusConstant.ROLE_SUPER_ADMIN);
        purchaseOrders.setOrderId(id);
        purchaseOrderMapper.update(purchaseOrders);
    }

    @Override
    public PurchaseOrderVo getById(Long id) {
        PurchaseOrders purchaseOrders = purchaseOrderMapper.getById(id);
        List<PurchaseOrderDetails> purchaseOrderDetails = purchaseOrderDetailMapper.getByOrderId(id);
        PurchaseOrderVo purchaseOrderVo = new PurchaseOrderVo();
        BeanUtils.copyProperties(purchaseOrders, purchaseOrderVo);
        purchaseOrderVo.setSupplierName(supplierMapper.getById(purchaseOrders.getSupplierId()).getSupplierName());
        purchaseOrderVo.setUserName(userMapper.getById(purchaseOrders.getUserId()).getUsername());
        List<PurchaseOrderDetailVo> purchaseOrderDetailVos = new ArrayList<>();
        for(PurchaseOrderDetails purchaseOrderDetail:purchaseOrderDetails){
            PurchaseOrderDetailVo purchaseOrderDetailVo = new PurchaseOrderDetailVo();
            BeanUtils.copyProperties(purchaseOrderDetail, purchaseOrderDetailVo);
            purchaseOrderDetailVo.setProductName(productMapper.getById(purchaseOrderDetail.getProductId()).getProductName());
            purchaseOrderDetailVos.add(purchaseOrderDetailVo);
        }
        purchaseOrderVo.setPurchaseOrderDetailVoList(purchaseOrderDetailVos);
        return purchaseOrderVo;
    }

    @Override
    public PageResult page(PurchaseOrderPageQueryDto purchaseOrderPageQueryDto) {
        PageHelper.startPage(purchaseOrderPageQueryDto.getPage(), purchaseOrderPageQueryDto.getPageSize());
        Page<PurchaseOrders> page = purchaseOrderMapper.page(purchaseOrderPageQueryDto);
        long total = page.getTotal();
        List<PurchaseOrders> records = page.getResult();
        List<PurchaseOrderWrapper> purchaseOrderWrappers = records.stream().map(record ->{
            String  supplierName = supplierMapper.getById(record.getSupplierId()).getSupplierName();
            String  userName = userMapper.getById(record.getUserId()).getUsername();
            return new PurchaseOrderWrapper(record, supplierName, userName);
        }).collect(Collectors.toList());
        return new PageResult(total, purchaseOrderWrappers);
    }

    @Override
    public void updateStatus(Long id) {
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        purchaseOrders.setStatus(StatusConstant.ENABLE);
        purchaseOrders.setOrderId(id);
        purchaseOrderMapper.update(purchaseOrders);
    }

    @Transactional
    @Override
    public void commit(StockInOrderDto stockInOrderDto) {
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        purchaseOrders.setStatus(StatusConstant.ROLE_USER);
        purchaseOrders.setOrderId(stockInOrderDto.getOrderId());
        purchaseOrderMapper.update(purchaseOrders);

        StockInOrders stockInOrders = new StockInOrders();
        BeanUtils.copyProperties(stockInOrderDto, stockInOrders);
        //1、订单类型为采购订单
        stockInOrders.setRelatedOrderType(StatusConstant.DISABLE);
        //2、总金额
        Double  totalAmount = 0.0;
        for(StockInOrderDetailsDto stockInOrderDetailsDto:stockInOrderDto.getStockInOrderDetailsDtoList())
            totalAmount += stockInOrderDetailsDto.getSubtotal();
        stockInOrders.setTotalAmount(totalAmount);
        //3、订单id
        stockInOrders.setRelatedOrderId(stockInOrderDto.getOrderId());
        stockInOrderMapper.insert(stockInOrders);

        StockInOrderDetails stockInOrderDetails = new StockInOrderDetails();
        stockInOrderDetails.setOrderId(stockInOrders.getOrderId());
        for(StockInOrderDetailsDto stockInOrderDetailsDto:stockInOrderDto.getStockInOrderDetailsDtoList()) {
            BeanUtils.copyProperties(stockInOrderDetailsDto, stockInOrderDetails);
            stockInOrderDetailMapper.insert(stockInOrderDetails);
        }
    }

}
